import axios from 'axios'
var baseURL = '/cheegu/api'// 车e估后端api
import store from '../store'
import router from '@/router'
import { Message } from 'element-ui'

export function fetchProxy(options, flg, msg) {
	return new Promise((resolve, reject) => {
		const instance = axios.create({
			timeout: 20000,
			baseURL,
			headers: flg ?  { 'Content-Type': 'application/json' } : { 'Content-Type': 'application/x-www-from-urlencoded;charset=utf-8' }
		})
		instance.interceptors.response.use( response => {
			let res = response.data
			if (res) {
				switch (res.success) {
					case 1:
					if (msg) {
					Message({
						message: msg,
						type: 'success',
						duration: 2 * 1000
					})
					}
					resolve(res)
					break
				}
			}
			return response
		},  err=> {
			Message({
				message: '呀，网络正在开小差，请重试',
				type: 'error',
				duration: 2 * 1000
			})
			reject(err)
			return Promise.reject(err)
		})
		instance(options)
	})
}